package com.android.lovingpet.community.search;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseActivity;
import com.android.lovingpet.common.userlist.UserListAdapter;
import com.android.lovingpet.bean.Account;

import java.util.List;

public class SearchActivity extends BaseActivity<ISearchContract.View, SearchPresenter> implements SearchView.OnQueryTextListener, ISearchContract.View {

    private UserListAdapter adapter;
    private ISearchContract.Presenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        ((TextView) findViewById(R.id.tv_title)).setText("查找");

        SearchView searchView = findViewById(R.id.sv_search);
        searchView.setIconifiedByDefault(false);
        searchView.setSubmitButtonEnabled(true);
        searchView.setQueryHint("查找");
        searchView.setOnQueryTextListener(this);

        adapter = new UserListAdapter();
        RecyclerView recyclerView = findViewById(R.id.rv_search);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        presenter = getPresenter();
    }

    @Override
    public SearchPresenter createPresenter() {
        return new SearchPresenter(this);
    }

    @Override
    public ISearchContract.View createView() {
        return this;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        presenter.search(query);
        return false;
    }
    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }

    @Override
    public void setData(final List<Account> accounts) {
        runOnUiThread(() -> {
            if (accounts != null) {
                adapter.add(accounts);
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void clearData() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.clear();
                adapter.notifyDataSetChanged();
            }
        });
    }

}
